'''
Created on Mar 16, 2014

@author: Eric
'''

class Block(object):
    
    def lines(self):
        pass
    
    def __str__(self):
        return '\n'.join(self.lines())

#######

class VerticalBlock(Block):
    
    def __init__(self, content):
        self.content = content
    
    def lines(self):
        for row in self.content:
            if isinstance(row, Block):
                for line in row.lines():
                    yield line
            else:
                yield row

VBlock = VerticalBlock

#######

class DiagonalBlock(Block):
    
    def __init__(self, content):
        self.content = content
    
    def lines(self):
        indent = ''
        for row in self.content:
            if isinstance(row, Block):
                lines = tuple(row.lines())
                if len(lines) > 1:
                    yield indent + lines[0]
                    indent = ' ' * len(indent)
                    for line in lines[1:-1]:
                        yield indent + line
                    indent += lines[-1]
                else:
                    indent += lines[0]
            else:
                indent += row
        yield indent

DBlock = DiagonalBlock

#######

class PrefixBlock(Block):
    
    def __init__(self, prefix, block):
        self.prefix = prefix
        self.block = block
    
    def lines(self):
        for line in self.block.lines():
            yield self.prefix + line

PBlock = PrefixBlock
